//
//  TravelShopViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/26.
//  Copyright © 2016年 KX. All rights reserved.
#import "Masonry.h"
#import "CellOfCollectionViewOfService.h"
#import "FreeTravelNetwork.h"
#import "TravelShopViewController.h"
#import "FreeTravelMacro.h"
#import "ModelOfTravelShopViewController.h"
#import "WebViewOfCommendViewController.h"
#import "CellOfHot_area.h"
#import "CellOfHot_goods.h"
#import "CellOfDiscount_topic.h"
#import "ModelOfHot_area.h"
#import "ModelOfDiscount_topic.h"
#import "ModelOfHot_goods.h"
#import "DetailsPageOfCellOfCollectionViewOfService.h"
#import "WebViewOfCommendViewController.h"
@interface TravelShopViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) ModelOfTravelShopViewController *model;
@property (nonatomic, strong) UICollectionView *collectionViewOfService;
@property (nonatomic, strong) NSArray *imageArrayOfcollectionViewOfService;
@property (nonatomic, strong) NSArray *labelTextArrayOfcollectionViewOfService;
@property (nonatomic, strong) UIButton *tiantiantejia;
@property (nonatomic, strong) UIButton *shandianjipiao;
@property (nonatomic, strong) UIButton *miaoqianzheng;
@end

@implementation TravelShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self handleData];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-108) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CellOfHot_area class] forCellReuseIdentifier:@"CellOfHot_area"];
     [self.tableView registerClass:[CellOfHot_goods class] forCellReuseIdentifier:@"CellOfHot_goods"];
     [self.tableView registerClass:[CellOfDiscount_topic class] forCellReuseIdentifier:@"CellOfDiscount_topic"];
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.tableView.tableHeaderView = self.headView;
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
    layout1.minimumLineSpacing = 0;
    layout1.minimumInteritemSpacing = 0;
    layout1.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout1.itemSize = CGSizeMake((SCREEN_WIDTH-20)/5, (SCREEN_WIDTH-20)/5+20);
    self.collectionViewOfService = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ((SCREEN_WIDTH-20)/5+20)*2+20) collectionViewLayout:layout1];
    [self.headView addSubview:self.collectionViewOfService];
    self.collectionViewOfService.backgroundColor = [UIColor whiteColor];
    self.collectionViewOfService.delegate = self;
    self.collectionViewOfService.dataSource = self;
    [self.collectionViewOfService registerClass:[CellOfCollectionViewOfService class] forCellWithReuseIdentifier:@"CellOfCollectionViewOfService"];
    self.imageArrayOfcollectionViewOfService = @[@"jijiutaocan",@"tejiajipiao",@"jiudian",@"jipiaosousuo",@"qianzheng",@"dangdiyou",@"menpiao",@"wifi",@"jiaotong",@"jiesongbaoche"];
    self.labelTextArrayOfcollectionViewOfService = @[@"机酒套餐",@"特价机票",@"酒店",@"机票搜索",@"签证",@"当地游",@"门票",@"WIFI电话",@"交通票券",@"接送包车"];
    
    self.tiantiantejia = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shandianjipiao = [UIButton buttonWithType:UIButtonTypeCustom];
    self.miaoqianzheng = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headView addSubview:_tiantiantejia];
    [self.headView addSubview:_shandianjipiao];
    [self.headView addSubview:_miaoqianzheng];
    [_tiantiantejia mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionViewOfService.mas_bottom);
        make.left.equalTo(self.headView.mas_left);
        make.height.mas_offset(100);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
    }];
    [self.tiantiantejia addTarget:self action:@selector(goTiantiantejia) forControlEvents:UIControlEventTouchUpInside];
    [_shandianjipiao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionViewOfService.mas_bottom);
        make.right.equalTo(self.headView.mas_right);
        make.height.mas_offset(50);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
    }];
    [self.shandianjipiao addTarget:self action:@selector(goShandianjipiao) forControlEvents:UIControlEventTouchUpInside];
    [_miaoqianzheng mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shandianjipiao.mas_bottom);
        make.right.equalTo(self.headView.mas_right);
        make.height.mas_offset(50);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.5);
    }];
    [self.miaoqianzheng addTarget:self action:@selector(goMiaoqianzheng) forControlEvents:UIControlEventTouchUpInside];
}
// 服务
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCollectionViewOfService *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfCollectionViewOfService" forIndexPath:indexPath];
    
    cell.label.text = self.labelTextArrayOfcollectionViewOfService[indexPath.row];
    cell.myimageView.image = [UIImage imageNamed:self.imageArrayOfcollectionViewOfService[indexPath.row]];
    return cell;
}
// 点击item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionViewOfService) {
        if (indexPath.row == 0 ) {
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 146;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1){
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 145;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 2){
            
        }else if (indexPath.row == 3){
            
            WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
            vc.url = @"http://m.ctrip.com/html5/flight/?allianceid=109013&sid=552815&tradeid=168";
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 4){
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 149;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 5){
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 173;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 6){
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 160;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 7){
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 163;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 8){
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 175;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            DetailsPageOfCellOfCollectionViewOfService *vc = [[DetailsPageOfCellOfCollectionViewOfService alloc]init];
            vc.type = 174;
            [self.navigationController pushViewController:vc animated:YES];
        }
       
    }
}
- (void)handleData{
    [FreeTravelNetwork GET:@"http://open.qyer.com/qyer/discount/zk/discount_index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=7.0.1&track_device_info=iPhone%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%25209.3.4&v=1" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model = [ModelOfTravelShopViewController modelWithDic:result];
        
        ModelOfMarket_topic *modelOftiantiantejia = self.model.data.market_topic[0];
        ModelOfMarket_topic *modelOfmiaoqianzheng = self.model.data.market_topic[1];
        ModelOfMarket_topic *modelOfshandianjipiao = self.model.data.market_topic[2];
        [self.tiantiantejia setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:modelOftiantiantejia.pic]]] forState:UIControlStateNormal];
        [self.miaoqianzheng setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:modelOfmiaoqianzheng.pic]]] forState:UIControlStateNormal];
        [self.shandianjipiao setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:modelOfshandianjipiao.pic]]] forState:UIControlStateNormal];
        [self.collectionViewOfService reloadData];
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CellOfHot_area *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfHot_area"];
        cell.model = self.model.data.hot_area[indexPath.row];
        cell.backgroundColor = [UIColor cyanColor];
        return cell;
    }else if (indexPath.row == 1){
        CellOfHot_area *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfHot_area"];
        cell.backgroundColor = [UIColor yellowColor];
        cell.model = self.model.data.hot_area[indexPath.row];
        return cell;
    }else if (indexPath.row == 2){
        CellOfHot_area *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfHot_area"];
        cell.backgroundColor = [UIColor greenColor];
        cell.model = self.model.data.hot_area[indexPath.row];
        return cell;
    }else if (indexPath.row == 3){
        CellOfDiscount_topic *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfDiscount_topic"];
        cell.model = self.model.data.discount_topic[0];
        return cell;
    }else if (indexPath.row == 4){
        CellOfDiscount_topic *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfDiscount_topic"];
        cell.model = self.model.data.discount_topic[1];
        return cell;
    }else if (indexPath.row == 5){
        CellOfHot_goods *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfHot_goods"];
        cell.model = self.model.data;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ModelOfHot_area *model = self.model.data.hot_area[0];
       
        return model.list.count * 100+(SCREEN_WIDTH-50)/4+50+20;
    }else if (indexPath.row == 1){
        ModelOfHot_area *model = self.model.data.hot_area[1];
        return model.list.count * 100+(SCREEN_WIDTH-50)/4+50+20;
    }else if (indexPath.row == 2){
        ModelOfHot_area *model = self.model.data.hot_area[2];
        return model.list.count * 100+(SCREEN_WIDTH-50)/4+50+20;
    }else if (indexPath.row == 3){
        ModelOfDiscount_topic *model = self.model.data.discount_topic[0];
        return model.list.count * 110+200;
    }else if (indexPath.row == 4){
        ModelOfDiscount_topic *model = self.model.data.discount_topic[1];
        return model.list.count * 110+200;
    }else if (indexPath.row == 5){
        return self.model.data.hot_goods.count/2 * 210 + self.model.data.hot_goods.count%2*210+50;
    }else{
        return 0;
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)goTiantiantejia{
    WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
    ModelOfMarket_topic *modelOftiantiantejia = self.model.data.market_topic[0];
    vc.url = modelOftiantiantejia.url;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)goShandianjipiao{
    WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
    ModelOfMarket_topic *modelOftiantiantejia = self.model.data.market_topic[2];
    vc.url = modelOftiantiantejia.url;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)goMiaoqianzheng{
    WebViewOfCommendViewController *vc = [[WebViewOfCommendViewController alloc]init];
    ModelOfMarket_topic *modelOftiantiantejia = self.model.data.market_topic[1];
    vc.url = modelOftiantiantejia.url;
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
